package com.xncoding.netty.socket.netty03;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.nio.ByteBuffer;
import java.util.List;

public class ByteDecoder extends ByteToMessageDecoder{
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
		int bsLen = buffer.readableBytes();
		byte[] bs = new byte[bsLen];
		buffer.readBytes(bs);
		//解析数据
		ByteBuffer bbf = ByteBuffer.allocate(bsLen);
		bbf = ByteBuffer.wrap(bs);
		// 第一位：
		String msg0 = bs[0] + "";
		// 第二位：
		String msg1 = bs[1] + "";
		// 第三位：
		String msg2 = bs[2] + "";
		//解析一段数据
		byte[] didByteArr = new byte[33];
		int j = 0;
		for (int i = 3; i < bsLen; i++) {
			didByteArr[j++] = bs[i];
		}
		String msg3 = new String(didByteArr, 0, j, "utf-8");

		String res = msg0 + msg1 + msg3; //此处可以封装成对象
		out.add(res);;
	}
}
